﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WF = CPPEI.WorkflowCore;

namespace Demo.BaseAuditAndReject
{
    public partial class Submit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid instanceId = new Guid(Request.QueryString["wf:instance"]);
                WF.Instance instance = WF.Engine.Get(instanceId);
                if (instance != null)
                {
                    StringBuilder builder = new StringBuilder();

                    foreach (var link in instance.State.Activity.Next)
                    {
                        builder.AppendFormat("{0},{1},", link.Action.Code, link.Action.Name);
                    }
                    _ctlHiddenWFStateId.Value = instance.State.Id.ToString();
                    _ctlHiddenWFActions.Value = builder.ToString();
                }
                else
                {
                    Response.Redirect("~/Error.aspx");
                }

                Bill bill = instance.Data as Bill;
                _ctlContent.Text = bill.Content;
                _ctlEditCreator.Text = bill.Creator;
                _ctlEditCreatedTime.Text = bill.CreatedTime.ToString();
            }

            string[] segments = _ctlHiddenWFActions.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int index = 0; index < segments.Length / 2; index++)
            {
                Button btnAction = new Button();
                btnAction.ID = string.Format("btn_{0}", segments[index * 2]);
                btnAction.Text = string.Format("{0}({1})", segments[index * 2], segments[index * 2 + 1]);
                btnAction.CommandName = segments[index * 2];
                btnAction.Click += btnAction_Click;
                this._ctlActionHolder.Controls.Add(btnAction);
            }
        }

        void btnAction_Click(object sender, EventArgs e)
        {
            Button btnAction = sender as Button;

            try
            {
                Guid instanceId = new Guid(Request.QueryString["wf:instance"]);
                Guid stateId = new Guid(this._ctlHiddenWFStateId.Value);
                Bill bill = new Bill();
                UIToModel(bill);

                WF.Instance instance = WF.Engine.Run(instanceId,
                    stateId,
                    btnAction.CommandName,
                    new WF.Actor("user01", "用户01", WF.ActorType.User),
                    new WF.Actor("user01", "用户01", WF.ActorType.User),
                    bill);

                string targetUrl = string.Format("{0}?wf:instance={1}", instance.State.Activity.Property.Page.Address, instance.Id);

                Response.Redirect(targetUrl);
            }
            catch (Exception ex)
            {
                //Response.Redirect("~/Error.aspx");
            }
        }

        private void UIToModel(Bill bill)
        {
            bill.Content = _ctlContent.Text;
            bill.CreatedTime = DateTime.Now;
            bill.Creator = _ctlEditCreator.Text;
        }
    }
}